import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登录注册组件
import Login from '../views/Login/Login.vue'
import Reg from '../views/Reg/Reg.vue'
import Main from '../views/Main/Main.vue'
import store from '../store'

Vue.use(VueRouter)
// 声明路由规则  name用于传参，一般都需要写，更规范
const routes = [
  { path: '/Main', name: 'Main', component: Main },
  { path: '/reg', name: 'Reg', component: Reg },
  { path: '/login', name: 'Login', component: Login }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/' && !store.state.token) {
    return next('/login')
  }
  next()
})
export default router
